package org.ogv.usada.models;

import org.ogv.OGV;
import org.ogv.tween.EaseFactory;
import org.ogv.tween.Tween;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Pool;

public class SlicedLeaf {

	private Tween _tweenX, _tweenY;
	private Sprite _s;
	private float _vAngular;
	
	public SlicedLeaf init(int leafId, float x, float y, float destX, float destY, float vAngular, int imgIdx) {
		float dur = 0.4f + GP.$().RANDOM.nextFloat() * 0.4f;
		_tweenX.init(x, destX, dur, EaseFactory.LINEAR);
		_tweenY.init(y, destY, dur, EaseFactory.IN_QUAD);
		_s = new Sprite(OGV.asset.getTextureRegion(Content.$().leaves[leafId].imageFiles[imgIdx]));
		_vAngular = vAngular;
		return this;
	}
	
	public void update(float delta) {
		_tweenX.update(delta);
		_tweenY.update(delta);
		_s.setPosition(_tweenX.getCurrentValue(), _tweenY.getCurrentValue());
		_s.rotate(_vAngular * delta);
	}
	
	public void draw(SpriteBatch sb) {
		_s.draw(sb);
	}
	
	public boolean hasDone() {
		return _tweenX.isDone() && _tweenY.isDone();
	}
	
	public Sprite getSprite() {
		return _s;
	}
	
	
	
	
	
	

	
	////Object Management
	private static Pool<SlicedLeaf> _POOL = new Pool<SlicedLeaf>() {
		@Override
		protected SlicedLeaf newObject() {
			return new SlicedLeaf();
		}
	};	
	public static void clearPool() 		{ _POOL.clear(); }
	public static SlicedLeaf obtain() 	{ return _POOL.obtain(); }
	public void free()					{  _POOL.free(this); } 
	private SlicedLeaf() {
		_tweenX = new Tween();
		_tweenY = new Tween();
	}
}
